package com.toubutu.audio.context;

import com.toubutu.audio.enums.RequestFrom;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/** request 工具
 * @author 戒躁
 * @date 2023/12/20
 */

public class HttpServletRequestContext {

    private static final String HEADER_FROM = "from";

    /**
     * 判断请求是否来自后台管理系统
     * @return
     */
    public static Boolean isFromAdmin(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String from = request.getHeader(HEADER_FROM);
        return RequestFrom.ADMIN.equals(RequestFrom.getFrom(from));
    }

    /**
     * 获取HttpServletRequest
     * @return
     */
    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}
